
Imports cmplib01


Public Class frmResourceConsumption

    Private ptrGame As CampaignerGameData
    Private ptrFact As Faction
    Private resourceItemEntries As New Hashtable


    Public Shadows Sub ShowDialog(ByRef game As CampaignerGameData, ByRef fact As Faction)

        ptrGame = game
        ptrFact = fact

        Populate()

        MyBase.ShowDialog()
    End Sub

    Private Sub CreateResourceEntries()

        resourceItemEntries = New Hashtable

        For Each skey As String In ptrGame.ResourceTypes_g.HT.Keys
            Dim rest As ResourceType = ptrGame.ResourceTypes_g.Item(skey)
            Dim rie As New ResourceItemEntry
            rie.resource = rest
            rie.constUse = 0
            rie.maxUse = 0
            rie.maxStore = 0
            rie.maxGen = 0
            resourceItemEntries.Add(rest.ResourceName, rie)
        Next
    End Sub


    Private Sub Populate()
        Try

            CreateResourceEntries()

            For Each mem As ChitMember In ptrGame.Chits
                If mem.OwningFaction.Equals(ptrFact) Then
                    If mem.UnitType = GroundChitClass_cgd.Objective Then
                        '// ok. We have an installation
                        Dim inst As InstallationChit = mem
                        For Each respo As ResourcePoolItem In inst.Resources
                            '// see if the resource exists in the hashtable, if not, create it. whoo. hoo.
                            Dim rie As ResourceItemEntry = resourceItemEntries.Item(respo.resourceTypeptr.ResourceName)
                            rie.maxGen += respo.accrueAmount
                            rie.maxStore += respo.maxAmount
                        Next
                        '// ok, now that we have the resource contributions, we can see how much we're able to soak up.
                        For Each rul As Rule In inst.chitRules
                            If TypeOf rul.ptrRule Is ResourceConsumingRuleType Then
                                '// find this rule type and start a-calculatin'!
                                Dim resrul As ResourceConsumingRuleType = rul.ptrRule
                                For Each rescost As ResourceCost In resrul.Costs
                                    Dim rie As ResourceItemEntry = resourceItemEntries.Item(rescost.ResourceTypeName)
                                    '// here we edit the costs 
                                    If resrul.AppliesDuring = RuleApplies_cgd.Constant Then
                                        rie.maxUse += rescost.ResourceCost
                                        rie.constUse += rescost.ResourceCost
                                    Else
                                        '// its a "non constant"
                                        rie.maxUse += rescost.ResourceCost
                                    End If
                                Next

                            End If
                        Next
                    End If
                End If
            Next

            List()

        Catch ex As Exception
            HandleException("frmResourceConsumption.Populate", ex)
        End Try
    End Sub

    Private Sub List()
        Dim rie As ResourceItemEntry
        lvwResources.Items.Clear()

        For Each skey As String In resourceItemEntries.Keys
            rie = resourceItemEntries.Item(skey)
            lvwResources.Items.Add(rie.AsListViewItem())

            If rie.maxGen = 0 And rie.maxUse > 0 Then rie.AsListViewItem.ForeColor = Color.Red
        Next
    End Sub

    Private Function ResourcebyName(ByVal name As String) As ResourceType
        Try
            Dim rest As ResourceType
            For Each skey As String In ptrGame.ResourceTypes_g.HT.Keys
                rest = ptrGame.ResourceTypes_g.Item(skey)
                If rest.ResourceName = name Then
                    Return rest
                End If
            Next
        Catch ex As Exception
            HandleException("frmResourceConsumption.ResourcebyName", ex)
        End Try
    End Function

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
    End Sub
End Class


Friend Class ResourceItemEntry
    Private _resource As ResourceType
    Private _maxgen As Integer
    Private _maxstore As Integer
    Private _maxuse As Integer
    Private _constuse As Integer
    Private _listviewitem As ListViewItem

    Public Sub New()
        _listviewitem = New ListViewItem

        _listviewitem.SubItems.Add("resource")
        _listviewitem.SubItems.Add("0")
        _listviewitem.SubItems.Add("0")
        _listviewitem.SubItems.Add("0")
        _listviewitem.SubItems.Add("0")

    End Sub

    Public Function AsListViewItem() As ListViewItem
        Return _listviewitem
    End Function

    Public Property resource() As ResourceType
        Get
            Return _resource
        End Get
        Set(ByVal value As ResourceType)
            _resource = value
            '_listviewitem.Text = _resource.ResourceName
            _listviewitem.SubItems.Item(0).Text = _resource.ResourceName
        End Set
    End Property

    Public Property maxGen() As Integer
        Get
            Return _maxgen
        End Get
        Set(ByVal value As Integer)
            _maxgen = value
            _listviewitem.SubItems.Item(1).Text = _maxgen
        End Set
    End Property

    Public Property maxStore() As Integer
        Get
            Return _maxstore
        End Get
        Set(ByVal value As Integer)
            _maxstore = value
            _listviewitem.SubItems.Item(2).Text = _maxstore
        End Set
    End Property

    Public Property maxUse() As Integer
        Get
            Return _maxuse
        End Get
        Set(ByVal value As Integer)
            _maxuse = value
            _listviewitem.SubItems.Item(3).Text = _maxuse
        End Set
    End Property

    Public Property constUse() As Integer
        Get
            Return _constuse
        End Get
        Set(ByVal value As Integer)
            _constuse = value
            _listviewitem.SubItems.Item(4).Text = _constuse
        End Set
    End Property

End Class